package club.xyes.zkh.ecommerce.service.adapter.impl;

import club.xyes.zkh.ecommerce.commons.entity.Shop;
import club.xyes.zkh.ecommerce.commons.entity.ShopMembershipLevel;
import club.xyes.zkh.ecommerce.commons.entity.User;
import club.xyes.zkh.ecommerce.commons.entity.UserMembership;
import club.xyes.zkh.ecommerce.commons.exception.ResourceNotFoundException;
import club.xyes.zkh.ecommerce.dao.mapper.UserMembershipMapper;
import club.xyes.zkh.ecommerce.service.adapter.UserMembershipSingleDependencyAdapter;
import club.xyes.zkh.ecommerce.service.impl.BaseServiceImpl;
import club.xyes.zkh.ecommerce.service.listener.OnUserMembershipLevelChangedListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by 郭文梁 2019/4/22 0022 09:42
 * UserMembershipSingleDependencyMapperAdapterImpl
 * 用户会员单依赖服务适配器
 *
 * @author 郭文梁
 * @data 2019/4/22 0022
 */
@Service("userMembershipSingleDependencyMapperAdapterImpl")
public class UserMembershipSingleDependencyMapperAdapterImpl extends BaseServiceImpl<UserMembership> implements UserMembershipSingleDependencyAdapter {
    private final UserMembershipMapper mapper;

    @Autowired
    public UserMembershipSingleDependencyMapperAdapterImpl(UserMembershipMapper mapper) {
        super(mapper);
        this.mapper = mapper;
    }

    @Override
    public UserMembership findByUserAndLevel(User user, ShopMembershipLevel level) {
        UserMembership query = new UserMembership();
        query.setUserId(user.getId());
        query.setMembershipLevelId(level.getId());
        return findOneByQuery(query);
    }

    @Override
    public List<UserMembership> findByUser(User user) {
        UserMembership query = new UserMembership();
        query.setUserId(user.getId());
        return findByQuery(query);
    }

    @Override
    public UserMembership getFetchAll(Integer id) {
        return mapper.selectByIdFetchAll(id);
    }

    @Override
    public UserMembership getRequiredFetchAll(Integer id) {
        UserMembership membership = getFetchAll(id);
        if (membership == null) {
            throw new ResourceNotFoundException(UserMembership.class, id);
        }
        return membership;
    }

    @Override
    public UserMembership register(User user, Shop shop,
                                   ShopMembershipLevel level,
                                   OnUserMembershipLevelChangedListener listener) {
        if (level == null) {
            //未找到符合条件的会员等级
            return null;
        }
        UserMembership membership = findByUserAndShop(user, shop, level.getShopMembershipId());

        if (membership == null) {
            membership = new UserMembership();
            membership.setUserId(user.getId());
            membership.setMembershipLevelId(level.getId());
            membership = save(membership);
            listener.onChanged(membership, user, shop, level, level.getMembership());
        } else {
            if (!membership.getMembershipLevelId().equals(level.getId())) {
                membership.setMembershipLevelId(level.getId());
                membership = updateById(membership);
                listener.onChanged(membership, user, shop, level, level.getMembership());
            }
        }
        membership.setUser(user);
        membership.setLevel(level);
        return membership;
    }

    @Override
    public UserMembership findByUserAndShop(User user, Shop shop, Integer shopMembershipId) {
        return mapper.selectOneByUserIdAndShopMembershipId(user.getId(), shopMembershipId);
    }
}
